package web;

import com.fasterxml.jackson.databind.ObjectMapper;
import entity.DeptEntity;
import service.DeptServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/dept/list")
public class DeptListServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        DeptServiceImpl service = new DeptServiceImpl();
        List<DeptEntity> list = service.getAll();

        ObjectMapper mapper = new ObjectMapper();
        String jsonList = mapper.writeValueAsString(list);

        resp.setContentType("application.json;charset=UTF-8");
        resp.getWriter().println(jsonList);
    }
}
